/*
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */
package org.geoscheduler.model;

import java.util.Date;
import java.util.List;

import org.geoscheduler.commons.marshaller.JSONField;

/**
 * Events.
 * 
 * @author paja
 * @author lko
 * @author srakyi
 * @version $Id: Event.java 196 2008-04-13 16:05:01Z lko $
 */
public class Event extends VersionedEntity implements Identifiable<Long> {

	public enum EventType {
		PRIVATE, PUBLIC;
	}

	public enum AlertType {
		LOCATION, TIME, LOCATION_TIME, LOCATION_OR_TIME, LOCATION_XOR_TIME, TAG, TAG_AND_TIME, TAG_OR_TIME
	}

	public enum RepeatType {
		ONCE, DAILY, WEEKLY, MONTHLY, YEARLY
	}

	private Long id;
	private Long idAccount;
	private int eventType;
	private String title;
	private String description;
	private Date timeCreated;
	private Date timeFrom;
	private Date timeTo;
	private Date lastAlert;
	private int alertType;
	private int repeatType;
	private Integer lastAlertLon;
	private Integer lastAlertLat;
	private String properties;
	private List<Tag> tags;

	private Location primaryLocation;
	private List<Location> extraLocations;

	public Event() {
		super();
	}

	public Event(Long id, Long idAccount, EventType eventType, String title, String description, Date timeCreated,
			RepeatType repeatType, Date timeFrom, Date timeTo, Date lastAlert, AlertType alertType, int lastAlertLon,
			int lastAlertLat, String properties) {
		this(id, idAccount, eventType.ordinal(), title, description, timeCreated, repeatType.ordinal(), timeFrom,
				timeTo, lastAlert, alertType.ordinal(), lastAlertLon, lastAlertLat, properties);
	}

	public Event(Long id, Long idAccount, int eventType, String title, String description, Date timeCreated,
			int repeatType, Date timeFrom, Date timeTo, Date lastAlert, int alertType, int lastAlertLon,
			int lastAlertLat, String properties) {
		super();
		this.id = id;
		this.idAccount = idAccount;
		this.eventType = eventType;
		this.title = title;
		this.description = description;
		this.timeCreated = timeCreated;
		this.repeatType = repeatType;
		this.timeFrom = timeFrom;
		this.timeTo = timeTo;
		this.lastAlert = lastAlert;
		this.alertType = alertType;
		this.lastAlertLon = lastAlertLon;
		this.lastAlertLat = lastAlertLat;
		this.properties = properties;
	}

	@JSONField
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	// this getter and setter is here just to make marshaller use version
	@Override
	@JSONField
	public synchronized Integer getVersion() {
		return super.getVersion();
	}

	@Override
	public synchronized void setVersion(Integer version) {
		super.setVersion(version);
	}

	@JSONField
	public Long getIdAccount() {
		return idAccount;
	}

	public void setIdAccount(Long idAccount) {
		this.idAccount = idAccount;
	}

	public int getEventType() {
		return eventType;
	}

	@JSONField(defaultValue = "PRIVATE")
	public EventType getEventTypeEnum() {
		return EventType.values()[eventType];
	}

	public void setEventTypeEnum(EventType eventType) {
		this.eventType = eventType.ordinal();
	}

	public void setEventType(int eventType) {
		this.eventType = eventType;
	}

	@JSONField
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@JSONField
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@JSONField
	public Date getTimeCreated() {
		return timeCreated;
	}

	public void setTimeCreated(Date timeCreated) {
		this.timeCreated = timeCreated;
	}

	@JSONField
	public Date getTimeFrom() {
		return timeFrom;
	}

	public void setTimeFrom(Date timeFrom) {
		this.timeFrom = timeFrom;
	}

	@JSONField
	public Date getTimeTo() {
		return timeTo;
	}

	public void setTimeTo(Date timeTo) {
		this.timeTo = timeTo;
	}

	@JSONField
	public Date getLastAlert() {
		return lastAlert;
	}

	public void setLastAlert(Date lastAlert) {
		this.lastAlert = lastAlert;
	}

	public int getAlertType() {
		return alertType;
	}

	public void setAlertType(int alertType) {
		this.alertType = alertType;
	}

	@JSONField(defaultValue = "LOCATION")
	public AlertType getAlertTypeEnum() {
		return AlertType.values()[alertType];
	}

	public void setAlertTypeEnum(AlertType alertType) {
		this.alertType = alertType.ordinal();
	}

	@JSONField
	public Integer getLastAlertLon() {
		return lastAlertLon;
	}

	public void setLastAlertLon(Integer lastAlertLon) {
		this.lastAlertLon = lastAlertLon;
	}

	@JSONField
	public Integer getLastAlertLat() {
		return lastAlertLat;
	}

	public void setLastAlertLat(Integer lastAlertLat) {
		this.lastAlertLat = lastAlertLat;
	}

	@JSONField
	public String getProperties() {
		return properties;
	}

	public void setProperties(String properties) {
		this.properties = properties;
	}

	public int getRepeatType() {
		return repeatType;
	}

	public void setRepeatType(int repeatType) {
		this.repeatType = repeatType;
	}

	@JSONField(defaultValue = "ONCE")
	public RepeatType getRepeatTypeEnum() {
		return RepeatType.values()[repeatType];
	}

	public void setRepeatTypeEnum(RepeatType repeatType) {
		this.repeatType = repeatType.ordinal();
	}

	@JSONField(elementType=Tag.class)
	public List<Tag> getTags() {
		return tags;
	}

	public void setTags(List<Tag> tags) {
		this.tags = tags;
	}

	@JSONField(elementType=Location.class)
	public List<Location> getExtraLocations() {
		return extraLocations;
	}

	public void setExtraLocations(List<Location> locations) {
		this.extraLocations = locations;
	}
	
	@JSONField
	public Location getLocation() {
		return primaryLocation;
	}
	
	public void setLocation(Location loc) {
		this.primaryLocation = loc;
	}
	
//	primary location setup
	
	private void checkPrimaryLocation() {
		if (this.primaryLocation == null) {
			this.primaryLocation = new Location();
		}
	}
	
	public void setLocId(Long id) {
		if (id != null) {
			checkPrimaryLocation();
			this.primaryLocation.setId(id);
		}
	}
	
	public void setLocLon(Integer lon) {
		if (lon != null) {
			checkPrimaryLocation();
			this.primaryLocation.setLon(lon);
		}
	}
	
	public void setLocLat(Integer lat) {
		if (lat != null) {
			checkPrimaryLocation();
			this.primaryLocation.setLat(lat);
		}
	}
	
	public void setLocRadius(Integer radius) {
		if (radius != null) {
			checkPrimaryLocation();
			this.primaryLocation.setRadius(radius);
		}
	}
}
